package com.dopplr.examples.modestmaps
{
    import com.dopplr.api.API;
    import com.dopplr.api.City;
    import com.dopplr.api.Traveller;
    import com.modestmaps.Map;
    import com.modestmaps.core.MapExtent;
    import com.modestmaps.geo.Location;
    import com.modestmaps.mapproviders.google.GoogleRoadMapProvider;
    
    import flash.display.BlendMode;
    import flash.display.LoaderInfo;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.filters.ColorMatrixFilter;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import com.modestmaps.extras.MapControls;

    [SWF(backgroundColor='#ffffff')]
    public class DopplrAreas extends Sprite
    {
        
        public var api:API;
        public var map:Map;
        
        public var bleedInner:Rectangle = new Rectangle(23, 23, 827, 331);
        public var safeInner:Rectangle = new Rectangle(71, 71, 732, 235);
        
        public function DopplrAreas()
        {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            var token:String = LoaderInfo(root.loaderInfo).parameters.token;
            if (token) {    
    
                api = new API(token);
                api.loadAuthenticatedUser(userLoaded);
                
                map = new Map(874, 378, true, new GoogleRoadMapProvider());
                map.setCenterZoom(new Location(0,0), 2);
                //map.grid.alpha = 0.1;
                addChild(map);

                 map.grid.filters = [
                    new ColorMatrixFilter(buildMatrixArray())
                ];

                var mooGuides:Sprite = new Sprite();
/*                 mooGuides.graphics.lineStyle(1,0x00ff00,1,true);
                mooGuides.graphics.beginFill(0xffffff,0);
                mooGuides.graphics.drawRect(bleedInner.x, bleedInner.y, bleedInner.width, bleedInner.height);
                mooGuides.graphics.lineStyle(1,0x00ff00,1,true);
                mooGuides.graphics.beginFill(0xffffff,0);
                mooGuides.graphics.drawRect(safeInner.x, safeInner.y, safeInner.width, safeInner.height); */
                addChild(mooGuides);
                
                mooGuides.addChild(new MapControls(map,true));
                                
                //stage.addEventListener(Event.RESIZE, function(event:Event):void { map.setSize(stage.stageWidth, stage.stageHeight); });
            }
            else {
                var text:TextField = new TextField();
                text.text = "problem initializing dopplr access: no token found";
                text.width = text.textWidth + 4;
                addChild(text);                
            }
            
        }
        
        public function userLoaded(user:Traveller):void
        {
            api.loadTravellerTrips(user, drawMe);
        }
        
        public function drawMe(user:Traveller):void
        {
            var extent:MapExtent = new MapExtent();
            var cities:Array = api.model.cities.sortOn('dayCount', Array.DESCENDING | Array.NUMERIC);
            for each (var city:City in cities)
            {
                var marker:Shape = new Shape();
                marker.blendMode = BlendMode.HARDLIGHT;
                marker.alpha = 0.8;
                marker.graphics.beginFill(city.rgb);
                 marker.graphics.lineStyle(2, 0x000000, 1, true);
//                marker.graphics.drawCircle(0,0,1+5*Math.sqrt(city.dayCount));
                var side:Number = 3 * city.dayCount;
                marker.graphics.drawRect(-side/2,-side/2, side, side);
                var location:Location = new Location(city.latitude, city.longitude);
                map.putMarker(city.geonameId.toString(), location, marker);
                extent.enclose(location);
            }
            map.setExtent(extent);
        }

        public static function buildMatrixArray(s:Number=0.0):Array 
        {
			const rwgt:Number = 0.3086;
			const gwgt:Number = 0.6094;
			const bwgt:Number = 0.0820;
			var ca:Number = (1.0-s)*rwgt + s;
			var cb:Number = (1.0-s)*rwgt;
			var cc:Number = (1.0-s)*rwgt;
			var cd:Number = (1.0-s)*gwgt;
			var ce:Number = (1.0-s)*gwgt + s;
			var cf:Number = (1.0-s)*gwgt;
			var cg:Number = (1.0-s)*bwgt;
			var ch:Number = (1.0-s)*bwgt;
			var ci:Number = (1.0-s)*bwgt + s;
			return [
			    ca,     cd,     cg,      0.0,	0.0,
		        cb,     ce,     ch,      0.0,	0.0,
        		cc,     cf,     ci,      0.0,	0.0,
		        0.0,    0.0,    0.0,     1.0,	0.0
			];
		}
		        
    }
}

